const userApi = require("../../../api/user.js");
const dateUtils = require("../../../utils/date.js");
Page({
  data: {
    pageType: "",
    userId: "",
    listData: [],
    testStatus: false,
    searchName: "",
    communityStyle: "",
    bottomPadding: "",
  },
  getMethod: function () {
    switch (this.data.pageType) {
      case "follow":
        return userApi.getListFollow({
          nickName: this.data.searchName,
        });
      case "followed":
        return userApi.listFollowed({
          nickName: this.data.searchName,
        });
      case "friend":
        return userApi.listFriend({
          nickName: this.data.searchName,
        });
      case "award":
        return userApi.listAward({
          userId: this.userId,
        });
      default:
        break;
    }
  },
  btnClick: function (e) {
    let type = e.currentTarget.dataset["type"];
    let idx = e.currentTarget.dataset["idx"];
    let uid = e.currentTarget.dataset["uid"];
    if (type === "follow") {
      this.data.listData[idx].status = !this.data.listData[idx].status;
      if (!this.data.listData[idx].status) {
        userApi
          .cancelFollow({
            followUserId: uid,
          })
          .then((res) => {
            this.init();
            if (res.code === 200) {
              wx.showToast({
                title: "已取关",
                duration: 2000,
              });
            }
          });
      } else {
        userApi
          .addFollow({
            followUserId: uid,
          })
          .then((res) => {
            this.init();
            if (res.code === 200) {
              wx.showToast({
                title: "关注成功！",
                duration: 2000,
              });
            }
          });
      }
    } else {
      userApi
        .addFollow({
          followUserId: uid,
        })
        .then((res) => {
          this.init();
          if (res.code === 200) {
            wx.showToast({
              title: "关注成功！",
              duration: 2000,
            });
          }
        });
    }
  },
  async profileClick(e) {
    // 判断是点击自己的图像 还是点击别人的图像
    let userId = wx.getStorageSync("loginInfo").userId;
    let info = e.currentTarget.dataset.info;
    let url =
      info[0] === userId
        ? "/pages/home/index"
        : "/pages/home/otherProfile/index";
    wx[info[0] === userId ? "switchTab" : "navigateTo"]({
      url: `${url}?userId=${info[0]}&nickName=${info[1]}`,
    });
  },
  articleClick(e) {
    wx.navigateTo({
      url: `/pages/community/detail/index?articleId=${e.currentTarget.dataset.item.awardPublishId}`,
    });
  },
  init() {
    this.getMethod().then((res) => {
      if (this.data.pageType !== "award") {
        let data = res.rows;
        for (let i = 0; i < data.length; i++) {
          data[i].status = true;
        }
        this.setData({
          listData: res.rows,
        });
      } else {
        let data = [];
        if (res && res.rows) {
          res.rows.forEach((el) => {
            el.publishContent = JSON.parse(el.publishContent);
            el.publishContent = el.publishContent.slice(0, 2);
            data.push(el);
          });
          this.setData({
            listData: data,
          });
        }
      }
    });
  },
  async onLoad(options) {
    this.setData({
      pageType: options.type,
    });
    switch (this.data.pageType) {
      case "follow":
        wx.setNavigationBarTitle({
          title: "关注",
        });
        break;
      case "followed":
        wx.setNavigationBarTitle({
          title: "粉丝",
        });
        break;
      case "friend":
        wx.setNavigationBarTitle({
          title: "好友",
        });
        break;
      case "award":
        wx.setNavigationBarTitle({
          title: "喜欢",
        });
        break;

      default:
        break;
    }
    this.setData({
      bottomPadding: getApp().globalData.bottomPadding,
      communityStyle: `padding-bottom: ${
        getApp().globalData.bottomPadding + 32
      }rpx`,
    });
    this.userId = wx.getStorageSync("loginInfo").userId;
    this.init();
  },
  searchListData(e) {
    let val = e.detail.value;
    this.setData({
      searchName: val,
    });
    this.init();
  },
});
